﻿using System;
using System.Text;
using ProdControls.NativePatterns.MessageConstants;
using ProdControls.Utility;

namespace ProdControls
{
    internal static class ProdWindowNative
    {
        /// <summary>
        ///   Close the specified window using the supplied window handle
        /// </summary>
        /// <param name="windowHandle"> Handle to the target window </param>
        /// <returns> If the function succeeds, the return value is true, otherwise, false </returns>
        internal static bool CloseWindow(IntPtr windowHandle)
        {
            if ((int) windowHandle == 0)
            {
                throw new ArgumentException("Handle cannot be zero");
            }
            NativeMethods.SendMessage(windowHandle, (int) WindowMessages.WM_CLOSE, 0, 0);
            return NativeMethods.DestroyWindow(windowHandle);
        }

        /// <summary>
        ///   maximizes specified window
        /// </summary>
        /// <param name="windowHandle"> Handle to the target window </param>
        internal static void MaximizeWindow(IntPtr windowHandle)
        {
            if ((int) windowHandle == 0)
            {
                throw new ArgumentException("Handle cannot be zero");
            }
            ShowWindow(windowHandle);
            NativeMethods.ShowWindowAsync(windowHandle, (int) ShowWindowCommand.SW_SHOWMAXIMIZED);
            NativeMethods.SetForegroundWindow(windowHandle);
        }

        /// <summary>
        ///   Minimizes specified window
        /// </summary>
        /// <param name="windowHandle"> Handle to the target window </param>
        internal static void MinimizeWindow(IntPtr windowHandle)
        {
            if ((int) windowHandle == 0)
            {
                throw new ArgumentException("Handle cannot be zero");
            }
            NativeMethods.ShowWindowAsync(windowHandle, (int) ShowWindowCommand.SW_SHOWMINNOACTIVE);
        }

        /// <summary>
        ///   Shows a window in its "normal" state.
        /// </summary>
        /// <param name="windowHandle"> Handle to the target window </param>
        internal static void ShowWindow(IntPtr windowHandle)
        {
            if ((int) windowHandle == 0)
            {
                throw new ArgumentException("Handle cannot be zero");
            }
            NativeMethods.ShowWindowAsync(windowHandle, (int) ShowWindowCommand.SW_SHOWDEFAULT);
        }

        /// <summary>
        ///   Retrieves the specified windows title
        /// </summary>
        /// <param name="windowHandle"> Handle to the target window </param>
        /// <returns> Title of the specified window, null if failure </returns>
        internal static string GetWindowTitle(IntPtr windowHandle)
        {
            StringBuilder sb = new StringBuilder();
            if (NativeMethods.GetWindowText(windowHandle, sb, sb.Capacity) == 0)
            {
                throw new ArgumentException("GetWindowText fail");
            }
            return sb.ToString();
        }

        /// <summary>
        ///   sets the title of the specified window
        /// </summary>
        /// <param name="windowHandle"> Handle to the window </param>
        /// <param name="newTitle"> Text to be used as the new title </param>
        internal static void SetWindowTitle(IntPtr windowHandle, string newTitle)
        {
            if ((int) windowHandle == 0)
            {
                throw new ArgumentException("Handle cannot be zero");
            }
            NativeMethods.SetWindowText(windowHandle, newTitle);
        }
    }
}